package com.gray.wow.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gray.wow.biz.ArticleBiz;
import com.gray.wow.biz.ContentBiz;
import com.gray.wow.common.Page;
import com.gray.wow.domain.Article;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.User;

@Controller
@RequestMapping("/article")
public class ArticleController {
	@Resource
	private ArticleBiz articleBiz;
	@Resource
	private ContentBiz contentBiz;

	@ExceptionHandler
	public String exp(Exception ex, HttpServletRequest request) {
		request.setAttribute("ex", ex);
		return "error";
	}

	@RequestMapping("/{articleId}")
	public String article(@PathVariable("articleId") Integer articleId,
			Model model) {
		Article article = articleBiz.getArticleById(articleId);
		if (article != null) {
			article.setViews(article.getViews() + 1);
			articleBiz.saveArticle(article);
			model.addAttribute(article);
			PageModel sameCategoryArticle = articleBiz.getArticle(5, 1,article.getCategory(), "views desc");
			model.addAttribute("sameCategoryArticle", sameCategoryArticle);
			return "article/article";
		}
		return null;
	}

	// 文章管理
	@RequestMapping("/manage")
	public String manage(HttpServletRequest request, Integer page,
			Integer pageSize) {
		User user = (User) request.getSession().getAttribute("admin");
		if (user != null) {
			PageModel pm = articleBiz.getArticle(Page.PAGESIZE_DEFAULT, page, null, null);
			request.setAttribute("pageModel", pm);
			return "admin/article/article";
		}
		return null;
	}

	@RequestMapping("/add")
	public String add(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			return "admin/article/saveArticle";
		}
		return null;
	}

	@RequestMapping("/edit")
	public String edit(Integer id, HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			Article article = articleBiz.getArticleById(id);
			if (article != null) {
				request.setAttribute("article", article);
			}

			return "admin/article/saveArticle";
		}
		return null;
	}

	@RequestMapping("/save")
	public String uploadArticle(HttpServletRequest request,
			HttpServletResponse response, Article article) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			if(articleBiz.saveArticle(article))
			out.print("success");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/delete")
	public String delArticle(int[] ids, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			User admin = (User) request.getSession().getAttribute("admin");
			if (admin != null) {
				if(articleBiz.delArticle(ids))
				out.print("success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
